
package com.chessimo.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class Utilities {
    
    
    public static String getTodayDate(String format) {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat(format); 
        String dateNow = formatter.format(currentDate.getTime());
        return dateNow;        
    }
    
    public static String getFutureDate(String format, int days) {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat(format); 
        currentDate.add(Calendar.DATE, days);
        String dateNow = formatter.format(currentDate.getTime());
        return dateNow;        
    }

    
    public static int getDaysBetween (String startDate, String endDate) {   
        

        int[] d1 = parseDate(startDate);
        int[] d2 = parseDate(endDate);
        
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        
        start.set(d1[2], d1[1]-1, d1[0]);
        end.set(d2[2], d2[1]-1, d2[0]);
        
        long m1 = start.getTimeInMillis();
        long m2 = end.getTimeInMillis();
        long diff = m2 - m1;
        int days = (int) (diff / (24 * 60 * 60 * 1000));
        
        return days;
        
    }
    
    /*
    // calc how many days the user is training
    public int calcTrainingDays (String iniDate) {       

        int[] arr = parseDate(iniDate);
        
        Calendar begin = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
        
        begin.set(arr[2], arr[1]-1, arr[0]);
        today.getTime();
        
        long m1 = begin.getTimeInMillis();
        long m2 = today.getTimeInMillis();
        long diff = m2 - m1;
        int days = (int) (diff / (24 * 60 * 60 * 1000));
        
        return days;
        
    }
    */ 
    
    public static int[] parseDate (String thisDate) {
        
        int[] arr = new int[3];
        
        arr[0] = Integer.valueOf(thisDate.substring(0,2)); // day
        arr[1] = Integer.valueOf(thisDate.substring(3,5)); // month
        arr[2] = Integer.valueOf(thisDate.substring(6,10)); // year

        return arr;    
        
    } 
    
    public static String mySubstring(String s, int ini, int bytes) {
        
        int leng = s.length();
        if (ini + bytes > leng || ini < 0) {
            return "";
        }
        
        String ret = s.substring(ini, ini + bytes);
        
        return ret;
    }   
    
    
    public static String[] explode (String txt, char delimiter) {
			
        char x;
	int ind = 0;
	int y;
	String str = "";
	String[] tab = new String[txt.length() / 2];
			
	for (int i=0; i < txt.length(); i++) {			
            x = txt.charAt(i);
            if (x == delimiter) {
		if (str.length() > 0) {
                    y = str.indexOf('.');
                    if (y < 0) {
                        tab[ind] = str;	
			ind++;
                    }
                    str = "";					
		}	
            } else {
		str = str + x;
            }		
	}
			
	if (str.length() > 0) {
            tab[ind] = str;
            ind++;
	}
			
	String[] tab2 = new String[ind];
	System.arraycopy(tab, 0, tab2, 0, ind);
			
	return tab2;
			
    } 
    
    
    
    public static String getContents(File file) {
       
       StringBuilder contents = new StringBuilder();
    
        try {
            BufferedReader input =  new BufferedReader(new FileReader(file));
            try {
                String line = null; 
                while (( line = input.readLine()) != null){
                    contents.append(line);
                }
            }
            finally {
                input.close();
            }
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
    
        return contents.toString();
  
   }
    
   
    public static String[] getContentsByLine(File file) {
       
        int ind = 0;
        String[] tab = new String[5000];

        StringBuilder contents = new StringBuilder();
    
        try {
            BufferedReader input =  new BufferedReader(new FileReader(file));
            try {
                String line = null; 
                while (( line = input.readLine()) != null){
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                    tab[ind] = line;
                    ind++;
                }
            }
            finally {
                input.close();
            }
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
    
        String[] tab2 = new String[ind];
	System.arraycopy(tab, 0, tab2, 0, ind);
        
        return tab2;
  
   }
    
    
    public void writeFile (String[] lines, File file) throws IOException  {
        
        if (!file.exists()) {
            file.createNewFile();
	}
        
        try {
            
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter out = new BufferedWriter(fw);
            
            for (int i=0; i < lines.length;i++) {
                out.write(lines[i] + "\r\n");    
            }
			
            out.close();
            

        } catch (Exception e){
            System.err.println("Error: " + e.getMessage());
        }
    
    }
    
   public static String[] getMonths(String lang) {
       
        // will be read from a file
        String[] m1 = {"Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"};
        String[] m2 = {"Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
       
        if (lang.equals("PT")) {
           return m1;    
        }
        
        if (lang.equals("EN")) {
           return m2;    
        }
        
       return null;
       
   }
   
   
   public String getMask(String piece) {
        
        if (piece.equals("K") || piece.equals("k")) {
            return "M";
        }    
            
        if (piece.equals("Q") || piece.equals("q")) {
            return "W";
        }    

        if (piece.equals("R") || piece.equals("r")) {
            return "T";
        }    

        if (piece.equals("B") || piece.equals("b") || piece.equals("L") || piece.equals("l")) {
            return "V";
        }    
            
        if (piece.equals("N") || piece.equals("n")) {
            return "S";
        }    

        if (piece.equals("P") || piece.equals("p")) {
            return "Z";
        }          
        
        return "";
        
    }
    
   
   public Boolean isNumeric (String c) {
        
        if (c == "0" || c == "1" || c == "2" || c == "3" || c == "4" || c == "5" || c == "6" || c == "7" || c == "8" || c == "9") {
            return true;
        } else {
            return false;
        }
        
    }
   
   public Boolean isInString(String myString, String myChar) {
    
        String c;
        int x = myString.length();
        
        for (int i=0; i < x; i++) {
            c = mySubstring(myString, i, 1);
            if(c.equals(myChar)) {
                return true;
            }
        }
    
        return false;
    
    }
   
   
   public String formatClock(int time) {
   
        int hrs = time / 60;
        int min = time - (hrs*60);
       
        String format = String.format("%%0%dd", 2);
        
        String sHrs = String.format(format, hrs);
        String sMin = String.format(format, min);
       
        return sHrs + ":" + sMin;
       
   }
   
   
   public int getSquare(int x, int y, int size) {
        
        int row = (int) Math.ceil(x / size);
        int col = (int) Math.ceil(y / size);
        
        if (row < 0 || row > y || col < 0 || col > 7) {
            return -1;
        }
        
        int square = (col*8) + row;
        return square;
        
    }
   
   
   
   // Get the position of the click on the table
   
   public int getOriginalX(int square, int size) {
        
        int x = (int) Math.ceil(square % 8);
        x = x * size;
        return x;
        
    }
    
    
    public int getOriginalY(int square, int size) {
        
        int y = (int) Math.ceil(square / 8);
        y = y * size + 105;

        return y;
        
    }
   
    public String getAlgebricSquare (int sq) {
        
        String tab[] = {"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
                        "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
                        "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
                        "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
                        "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
                        "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
                        "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
                        "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"};
        
        return tab[sq];
     
    }
    
    public String getAlgebricSquareInverted (int sq) {
        
        String tab[] = {"h1", "g1", "f1", "e1", "d1", "c1", "b1", "a1",
                        "h2", "g2", "f2", "e2", "d2", "c2", "b2", "a2",
                        "h3", "g3", "f3", "e3", "d3", "c3", "b3", "a3",
                        "h4", "g4", "f4", "e4", "d4", "c4", "b4", "a4",
                        "h5", "g5", "f5", "e5", "d5", "c5", "b5", "a5",
                        "h6", "g6", "f6", "e6", "d6", "c6", "b6", "a6",
                        "h7", "g7", "f7", "e7", "d7", "c7", "b7", "a7",
                        "h8", "g8", "f8", "e8", "d8", "c8", "b8", "a8"};
        
        return tab[sq];
     
    }
    
    public static String formatMovesBox(String[] moves, String firstColor) {
        
        int tot_moves = moves.length;
        String[] tab = new String[tot_moves + 2];
        
        if (firstColor.equals("b")) {
            tab[0] = "...";
            for (int i=0; i < tot_moves; i++) {
                tab[i+1] = moves[i];
            }
            tab[tab.length - 1] = "";
        } else {
            for (int i=0; i < tot_moves; i++) {
                tab[i] = moves[i];
            }
            tab[tab.length - 2] = "";
            tab[tab.length - 1] = "";
        }
        
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<b>Jogadas realizadas</b><br>");
        sb.append("<table width='180'>");
     
        for (int i=0; i < tab.length - 1; i=i+2) {
            int line = i / 2 + 1;
            sb.append("<tr><td width='10%'>" + line + ".</td>");    
            sb.append("<td>" + tab[i] + "</td>");
            sb.append("<td>" + tab[i+1] + "</td>");
            sb.append("</tr>");       
        }
     
        sb.append("</table><html>");
     
        return sb.toString();
     
    }
    
}
